#version 330 core
out vec4 FragColor;

in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoord;

uniform vec3 viewPos;
uniform int zebra;
uniform float alpha;
uniform samplerCube skybox;

// 材质（不要使用函数名作为变量名）
struct Material
{
    sampler2D diffuse;
    sampler2D specular;
    sampler2D reflect_;
    sampler2D reflact;
};

// 定向光
struct DirLight
{
    vec3 direction;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

// 点光源
struct PointLight
{
    vec3 position;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;

    float constant;
    float linear;
    float quadratic;
};

// 聚光
struct SpotLight
{
    vec3 position;
    vec3 direction;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;

    float constant;
    float linear;
    float quadratic;

    float cutOff;
    float outerCutOff;
};

vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);

// 控制点光源和聚光的数量
#define NR_POINT_LIGHT 4
#define NR_SPOT_LIGHT 4

uniform Material material;
uniform DirLight dirLight;
uniform PointLight pointLights[NR_POINT_LIGHT];
uniform SpotLight spotLights[NR_SPOT_LIGHT];

void main()
{
    vec3 norm = normalize(Normal);
    vec3 viewDir = normalize(viewPos - FragPos);

    // 定向光照
    vec3 result = CalcDirLight(dirLight, norm, viewDir);

    // 如果没有斑马纹，就叠加光源，否则根据定向光照绘制斑马纹
    if (zebra == 0)
    {
        // 叠加点光源，根据常数项判定有没有光源
        for (int i = 0; i < NR_POINT_LIGHT; i++)
            if (pointLights[i].constant != 0)
                result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);

        // 叠加聚光，根据常数项判定有没有光源
        for (int i = 0; i < NR_SPOT_LIGHT; i++)
            if (spotLights[i].constant != 0)
                result += CalcSpotLight(spotLights[i], norm, FragPos, viewDir);

        // 计算反射
        vec3 I = normalize(FragPos - viewPos);
        vec3 R = reflect(I, norm);
        result += texture(skybox, R).rgb * vec3(texture(material.reflect_, TexCoord));

        // 计算折射
        float ratio = 1.00 / 1.52;
        R = refract(I, norm, ratio);
        result += texture(skybox, R).rgb * vec3(texture(material.reflact, TexCoord));
    }

    // 执行 gamma 校正
    float gamma = 2.2;
    vec4 texColor = texture(material.diffuse, TexCoord);
    FragColor = pow(vec4(result, texColor.a), vec4(1.0 / gamma));
    FragColor.a = alpha;
}

vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
    vec3 lightDir = normalize(-light.direction);

    // 计算夹角
    vec3 diffuseColor = vec3(texture(material.diffuse, TexCoord));

    // 根据斑马纹设置
    if (zebra > 0)
    {
        float diff = abs(dot(normal, viewDir));
        int d = int(diff * zebra);
        if (d % 2 == 0)
            diffuseColor = vec3(1, 1, 1);
        else
            diffuseColor = vec3(0, 0, 0);
        return diffuseColor;
    }

    // 环境光反射
    vec3 ambient = light.ambient * diffuseColor;

    // 漫反射
    float diff = max(dot(normal, lightDir), 0.0);
    vec3 diffuse = light.diffuse * diff * diffuseColor;

    // 镜面反射（Phong）
    // vec3 reflectDir = reflect(-lightDir, normal);
    // float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);

    // 镜面反射（Blinn-Phong）
    vec3 middle = normalize(lightDir + viewDir);
    float spec = pow(max(dot(middle, normal), 0.0), 64.0);

    vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoord));

    // 合并结果
    return (ambient + diffuse + specular);
}

vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
    vec3 lightDir = normalize(light.position - fragPos);

    // 计算距离衰减
    float dis = length(light.position - fragPos);
    float attenuation = 1.0 / (light.constant + light.linear * dis + light.quadratic * (dis * dis));

    // 环境光反射
    vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoord));

    // 漫反射
    float diff = max(dot(normal, lightDir), 0.0);
    vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoord));

    // 镜面反射（Phong）
    // vec3 reflectDir = reflect(-lightDir, normal);
    // float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);

    // 镜面反射（Blinn-Phong）
    vec3 middle = normalize(lightDir + viewDir);
    float spec = pow(max(dot(middle, normal), 0.0), 32.0);

    vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoord));

    // 合并结果
    return (ambient + diffuse + specular) * attenuation;
}

vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
    vec3 lightDir = normalize(light.position - fragPos);

    // 计算距离衰减
    float dis = length(light.position - fragPos);
    float attenuation = 1.0 / (light.constant + light.linear * dis + light.quadratic * (dis * dis));

    // 环境光反射
    vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoord));

    // 漫反射
    float diff = max(dot(normal, lightDir), 0.0);
    vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoord));

    // 镜面反射（Phong）
    // vec3 reflectDir = reflect(-lightDir, normal);
    // float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);

    // 镜面反射（Blinn-Phong）
    vec3 middle = normalize(lightDir + viewDir);
    float spec = pow(max(dot(middle, normal), 0.0), 32.0);

    vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoord));

    // 计算夹角余弦，比较传入的余弦
    float theta = dot(lightDir, normalize(-light.direction));
    float epsilon = light.cutOff - light.outerCutOff;

    // clamp 函数约束取值范围
    float insensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);

    // 合并结果
    return (ambient + diffuse * insensity + specular * insensity) * attenuation;
}